package com.han.hotelplat.controller.bill;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.ResponseWrapTo.CodeType;
import com.han.hotelplat.comm.utils.ListSortUtil;
import com.han.hotelplat.pojo.bill.Bill;
import com.han.hotelplat.pojo.cashextract.CashExtract;
import com.han.hotelplat.pojo.employee.Employee;
import com.han.hotelplat.pojo.hotel.Hotel;
import com.han.hotelplat.pojo.hotelmanager.Hotelmanager;
import com.han.hotelplat.pojo.systemuser.Systemuser;
import com.han.hotelplat.service.bill.BillService;
import com.han.hotelplat.service.cashextract.CashExtractService;
import com.han.hotelplat.service.hotelmanager.HotelmanagerService;

@Controller
@RequestMapping("/bill/*")
public class BillController {
	@Autowired
	private BillService billService;
	@Autowired
	HotelmanagerService hotelmanagerService;
	@Autowired
	private CashExtractService cashExtractService;
	
	@ResponseBody
	@RequestMapping(value = "getMyBill", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String getMyBill(HttpServletRequest request,Integer userType) {
		String userid= "";//钱包的用户ID
		if(userType.equals(Constant.USER_TYPE_EMPLOYEE)){
			Employee empu = (Employee) request.getSession().getAttribute(Constant.SESSION_LOGIN_USER);
			if(empu == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = empu.getUserid();//帮工的钱包用户ID储存的是帮工的用户ID
		}
		if(userType.equals(Constant.USER_TYPE_HOTELMANAGER)){
			Hotelmanager systemu = (Hotelmanager) request.getSession().getAttribute(Constant.SESSION_LOGIN_USER);
			if(systemu == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = systemu.getHotelid().toString();//酒店的钱包用户ID存储的是酒店ID
		}
		if(userType.equals(Constant.USER_TYPE_PLATFORM)){
			Systemuser systemu = (Systemuser) request.getSession().getAttribute(Constant.SESSION_LOGIN_USER);
			if(systemu == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			 userid = Constant.WALLET_ID.toString();//平台的钱包用户ID存储的是固定值
		}
		
		List<Bill> data = billService.findMyBill(userid);
		
		Map<String, List<Bill>> billMap = new HashMap<String, List<Bill>>();
		List<Bill>otherBill = new ArrayList<Bill>();
		for(Bill bill:data){
			//如果是提现类型的账单
			if(bill.getType().intValue() == Constant.BILL_TYPE_CASH.intValue()){
				String[] billInfor = bill.getTaskid().split("=");
				Integer state = Integer.parseInt(billInfor[1]);
				bill.setState(state);
			}
			if(bill.getType().intValue() == Constant.BILL_TYPE_TASK.intValue()
					&& userType.equals(Constant.USER_TYPE_EMPLOYEE)){
				List<Bill> blist = billMap.get(bill.getTaskid());
				if(blist == null){
					blist = new ArrayList<Bill>();
				}
				blist.add(bill);
				billMap.put(bill.getTaskid(), blist);
			}else{
				otherBill.add(bill);
			}
		}
		if(billMap.size() != 0){
			data.clear();
			Iterator<Entry<String, List<Bill>>> billIts = billMap.entrySet().iterator();
			while (billIts.hasNext()) {
				Bill bill = new Bill();
				Entry<String, List<Bill>> billEn = billIts.next();
				List<Bill> li = billEn.getValue();
				String taskid = billEn.getKey();
				float amount = 0;
				Date createtime = null;
				for(Bill b : li){
					amount = amount + b.getAmount();
					if(createtime == null || b.getCreatetime().getTime()>createtime.getTime()){
						createtime = b.getCreatetime();
					}
				}
				bill.setTaskid(taskid);
				bill.setAmount(amount);
				bill.setCreatetime(createtime);
				bill.setType(Constant.BILL_TYPE_TASK);
				bill.setChargechannel(Constant.CHARGE_CHANNEL_TRANSFER);
				data.add(bill);
			}
			data.addAll(otherBill);
			ListSortUtil<Bill> sort = new ListSortUtil<Bill>();
			sort.sort(data, "createtime", false);
		}
		return new ResponseWrapTo(CodeType.success,"数据获取成功",data).toJson();
	}
}
